package com.hospital.workstation.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.hospital.workstation.domain.vo.MedicalRecordVo;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.hospital.common.annotation.RepeatSubmit;
import com.hospital.common.annotation.Log;
import com.hospital.common.core.controller.BaseController;
import com.hospital.common.core.domain.PageQuery;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.validate.AddGroup;
import com.hospital.common.core.validate.EditGroup;
import com.hospital.common.core.validate.QueryGroup;
import com.hospital.common.enums.BusinessType;
import com.hospital.common.utils.poi.ExcelUtil;
import com.hospital.workstation.domain.vo.HisMedicalOrdersVo;
import com.hospital.workstation.domain.bo.HisMedicalOrdersBo;
import com.hospital.workstation.service.IHisMedicalOrdersService;
import com.hospital.common.core.page.TableDataInfo;

/**
 * 医嘱
 *
 * @author whb
 * @date 2023-03-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/medicalOrders")
public class HisMedicalOrdersController extends BaseController {

    private final IHisMedicalOrdersService iHisMedicalOrdersService;

    /**
     * 查询医嘱列表
     */
    @SaCheckPermission("workstation:medicalOrders:list")
    @GetMapping("/list")
    public TableDataInfo<HisMedicalOrdersVo> list(HisMedicalOrdersBo bo, PageQuery pageQuery) {
        return iHisMedicalOrdersService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询HIS医嘱用药列表
     *
     * @param medicalRecordNo 药历号
     * @return
     */
    @GetMapping("/list/{medicalRecordNo}")
    public R<List<MedicalRecordVo>> queryListByMedicalRecordNo(@PathVariable Integer medicalRecordNo) {
        return iHisMedicalOrdersService.queryListByMedicalRecordNo(medicalRecordNo);
    }

    /**
     * 查询HIS医嘱用药列表（分组）
     *
     * @param medicalRecordNo 病历号
     * @param category        类别（1长嘱 2临嘱 3中药饮片）
     * @return
     */
    @GetMapping("/list/{medicalRecordNo}/{category}")
    public R<List<MedicalRecordVo>> queryOrdersListToGroup(@PathVariable Integer medicalRecordNo, @PathVariable String category) {
        return iHisMedicalOrdersService.queryOrdersListToGroup(medicalRecordNo, category);
    }

    /**
     * 获取医嘱详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("workstation:medicalOrders:query")
    @GetMapping("/{id}")
    public R<HisMedicalOrdersVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        return R.ok(iHisMedicalOrdersService.queryById(id));
    }

    /**
     * 新增医嘱
     */
    @SaCheckPermission("workstation:medicalOrders:add")
    @Log(title = "医嘱", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HisMedicalOrdersBo bo) {
        return toAjax(iHisMedicalOrdersService.insertByBo(bo));
    }

    /**
     * 修改医嘱
     */
    @SaCheckPermission("workstation:medicalOrders:edit")
    @Log(title = "医嘱", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HisMedicalOrdersBo bo) {
        return toAjax(iHisMedicalOrdersService.updateByBo(bo));
    }

    /**
     * 删除医嘱
     *
     * @param ids 主键串
     */
    @SaCheckPermission("workstation:medicalOrders:remove")
    @Log(title = "医嘱", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iHisMedicalOrdersService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
